﻿using MediatR;
using System.WXT.Infrstructre.Interface;
using System.WXT.WebAPI.Application.Command.CraftCommand;

namespace System.WXT.WebAPI.Application.CommandHandlers.CraftCommandHandlers
{
    public class CraftReturnIDAddedCommandHandlers:IRequestHandler<CraftReturnIDAddedCommand, int>
    {
        protected readonly ICraftRepository craftRepository;
        protected readonly ICraftManageProcessRepository craftManageProcessRepository;

        public CraftReturnIDAddedCommandHandlers(ICraftRepository craftRepository, ICraftManageProcessRepository craftManageProcessRepository)
        {
            this.craftRepository = craftRepository;
            this.craftManageProcessRepository = craftManageProcessRepository;
        }
        /// <summary>
        /// 添加返回ID
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<int> Handle(CraftReturnIDAddedCommand request, CancellationToken cancellationToken)
        {
            int id = await craftRepository.AddAsyncs(new Domain.Craft()
            {
                craftManagenumbering = request.craftManagenumbering,
                craftManageName = request.craftManageName,
                craftManageState = request.craftManageState,
                craftManageFinishName = request.craftManageFinishName,
                craftManageDate = request.craftManageDate,
                craftManageAddName = request.craftManageAddName
            });
            foreach(var item in request.craftManageProcessId)
            {
                await craftManageProcessRepository.AddAsyncs(new Domain.CraftManageProcess()
                {
                    craftManageId = id,//工艺ID
                    ProcessId = item//工序ID
                });
            }
            return 1;
        }
    }
}
